<?php

namespace App\Console\Commands;

use App\Models\AttendanceSet;
use App\Models\Company;
use App\Models\Feedback;
use App\Models\FeedbackDetail;
use App\Models\FeedbackSetDetail;
use App\Models\RewardPunishSet;
use App\Models\RewardPunishSetDetail;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Redis;
use Illuminate\Support\Facades\DB;


class AutoCreateSet extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'add_set';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '自动创建公司年度设置';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //年终创建下一年的设置
        $nowtime = date('md');
        $year_param = date('Y');
        $companys = Company::all();
        $default = array(
            '很好'=>'5',
            '较好'=>'4',
            '正常'=>'3',
            '较差'=>'2',
            '很差'=>'1',
        );
        $names = array('主动性','学习能力','合作精神','职业操守','专业技能');
        foreach ($companys as $c){
            $punish_set = RewardPunishSet::where('corpid',$c->corpid)->where('year_param',$year_param)->where('kind','2')->first();
            if(empty($punish_set) && $c->from_year > 0){
                DB::beginTransaction();
                try{
                    foreach ($names as $k=>$n){
                        $feedback = new Feedback();
                        $feedback->corpid = $c->id;
                        $feedback->name = $n;
                        $feedback->year_param = $year_param;
                        $feedback->detail = serialize($default);
                        $feedback->save();

                        foreach ($default as $ky=>$v){
                            $set_detail = new FeedbackSetDetail();
                            $set_detail->set_id = $feedback->id;
                            $set_detail->set_name = $ky;
                            $set_detail->set_value = $v;
                            $set_detail->save();
                        }
                    }

                    $default_jc = array(
                        '高级'=>'5',
                        '中级'=>'3',
                        '低级'=>'1',
                    );

                    $reward = new RewardPunishSet();
                    $reward->kind = 1;
                    $reward->year_param = $year_param;
                    $reward->corpid = $c->id;
                    $reward->detail = serialize($default_jc);
                    $reward->save();
                    foreach ($default_jc as $ky=>$vy){
                        $set = new RewardPunishSetDetail();
                        $set->set_id = $reward->id;
                        $set->set_name = $ky;
                        $set->set_value = $vy;
                        $set->save();
                    }

                    $punish = new RewardPunishSet();
                    $punish->kind = 2;
                    $punish->year_param = $year_param;
                    $punish->corpid = $c->id;
                    $punish->detail = serialize($default_jc);
                    $punish->save();
                    foreach ($default_jc as $ky=>$vy){
                        $set = new RewardPunishSetDetail();
                        $set->set_id = $punish->id;
                        $set->set_name = $ky;
                        $set->set_value = $vy;
                        $set->save();
                    }

                    $att_set = new AttendanceSet();
                    $att_set->corpid = $c->id;
                    $att_set->year_param = $year_param;
                    $att_set->save();
                    
                    $c->to_year = $year_param;
                    $c->save();
                   
                }catch (\PDOException $e) {
                    DB::rollback();
                    Log::info($e->getMessage());
                }
                DB::commit();
            }
            
            $next_year_param = $year_param + 1;
            $next_punish_set = RewardPunishSet::where('corpid',$c->corpid)->where('year_param',$next_year_param)->where('kind','2')->first();
            if(empty($next_punish_set) && $c->from_year > 0){
                DB::beginTransaction();
                try{
                    foreach ($names as $k=>$n){
                        $feedback = new Feedback();
                        $feedback->corpid = $c->id;
                        $feedback->name = $n;
                        $feedback->year_param = $next_year_param;
                        $feedback->detail = serialize($default);
                        $feedback->save();

                        foreach ($default as $ky=>$v){
                            $set_detail = new FeedbackSetDetail();
                            $set_detail->set_id = $feedback->id;
                            $set_detail->set_name = $ky;
                            $set_detail->set_value = $v;
                            $set_detail->save();
                        }
                    }

                    $default_jc = array(
                        '高级'=>'5',
                        '中级'=>'3',
                        '低级'=>'1',
                    );

                    $reward = new RewardPunishSet();
                    $reward->kind = 1;
                    $reward->year_param = $next_year_param;
                    $reward->corpid = $c->id;
                    $reward->detail = serialize($default_jc);
                    $reward->save();
                    foreach ($default_jc as $ky=>$vy){
                        $set = new RewardPunishSetDetail();
                        $set->set_id = $reward->id;
                        $set->set_name = $ky;
                        $set->set_value = $vy;
                        $set->save();
                    }

                    $punish = new RewardPunishSet();
                    $punish->kind = 2;
                    $punish->year_param = $next_year_param;
                    $punish->corpid = $c->id;
                    $punish->detail = serialize($default_jc);
                    $punish->save();
                    foreach ($default_jc as $ky=>$vy){
                        $set = new RewardPunishSetDetail();
                        $set->set_id = $punish->id;
                        $set->set_name = $ky;
                        $set->set_value = $vy;
                        $set->save();
                    }

                    $att_set = new AttendanceSet();
                    $att_set->corpid = $c->id;
                    $att_set->year_param = $next_year_param;
                    $att_set->save();

                    $c->to_year = $next_year_param;
                    $c->save();

                }catch (\PDOException $e) {
                    DB::rollback();
                    Log::info($e->getMessage());
                }
                DB::commit();
            }
            
        }
    }
    



}
